/**
 *  *2010-5-15 下午02:11:49  add 
 */
package com.base.myproject.client.HR.bcode;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.base.myproject.client.busi.BaseStore;
import com.base.myproject.client.busi.BaseTreeStore;
import com.base.myproject.client.busi.BO.BusiObject;
import com.base.myproject.client.busi.print.PrintBcodeDialog;
import com.base.myproject.client.jdbc.DataSet;
import com.base.myproject.client.jdbc.StoreInfo;
import com.base.myproject.client.system.BaseResource;
import com.base.myproject.client.system.Env;
import com.base.myproject.client.tools.CommandSyncContainer;
import com.base.myproject.client.tools.CommandSyncReturnObject;
import com.base.myproject.client.tools.CommandSyncsql;
import com.base.myproject.client.tools.GreetingService;
import com.base.myproject.client.tools.GreetingServiceAsync;
import com.base.myproject.client.tools.Tools;
import com.base.myproject.client.ui.BaseJDBCPanel;
import com.extjs.gxt.ui.client.GXT;
import com.extjs.gxt.ui.client.Style.Scroll;
import com.extjs.gxt.ui.client.data.BaseTreeModel;
import com.extjs.gxt.ui.client.data.ModelData;
import com.extjs.gxt.ui.client.data.ModelStringProvider;
import com.extjs.gxt.ui.client.data.ModelType;
import com.extjs.gxt.ui.client.data.TreeModel;
import com.extjs.gxt.ui.client.event.BaseEvent;
import com.extjs.gxt.ui.client.event.Events;
import com.extjs.gxt.ui.client.event.Listener;
import com.extjs.gxt.ui.client.event.MenuEvent;
import com.extjs.gxt.ui.client.event.MessageBoxEvent;
import com.extjs.gxt.ui.client.event.SelectionListener;
import com.extjs.gxt.ui.client.event.TreePanelEvent;
import com.extjs.gxt.ui.client.store.Store;
import com.extjs.gxt.ui.client.widget.ContentPanel;
import com.extjs.gxt.ui.client.widget.HorizontalPanel;
import com.extjs.gxt.ui.client.widget.Info;
import com.extjs.gxt.ui.client.widget.Label;
import com.extjs.gxt.ui.client.widget.MessageBox;
import com.extjs.gxt.ui.client.widget.TabItem;
import com.extjs.gxt.ui.client.widget.TabPanel;
import com.extjs.gxt.ui.client.widget.button.Button;
import com.extjs.gxt.ui.client.widget.form.CheckBox;
import com.extjs.gxt.ui.client.widget.form.StoreFilterField;
import com.extjs.gxt.ui.client.widget.menu.Menu;
import com.extjs.gxt.ui.client.widget.menu.MenuItem;
import com.extjs.gxt.ui.client.widget.toolbar.FillToolItem;
import com.extjs.gxt.ui.client.widget.toolbar.SeparatorToolItem;
import com.extjs.gxt.ui.client.widget.toolbar.ToolBar;
import com.extjs.gxt.ui.client.widget.treepanel.TreePanel;
import com.extjs.gxt.ui.client.widget.treepanel.TreePanel.CheckCascade;
import com.extjs.gxt.ui.client.widget.treepanel.TreePanel.CheckNodes;
import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.Random;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.AbstractImagePrototype;
import com.google.gwt.user.client.ui.Widget;

/**
 * @author Administrator 2010-5-15 javazgw@gmail.com update cxghiu410@gmail.com
 *         雇员的主页面
 */

public class BcodeMain extends BaseJDBCPanel {
	String treeDepId = null; // 将当前树形结构中选中的部门name保存
	String treeDepName = null;
	TreePanel<TreeModel> treepanel = null;
	public static BaseStore<BusiObject> store = null;
	QueryBcodeDialog cbd = new QueryBcodeDialog();
	ToolBar toolbar = new ToolBar();

	TreeModel select = null; // 保存选中的treeModel
	private CheckBox cb = null;
	public TabPanel tabpanel;

	protected BaseModelStringProvider<TreeModel> defaultStringProvider = new BaseModelStringProvider<TreeModel>();
	// 用于显示tree中的数据，修改树的显示为name+code;

	String[] querystr = new String[] { "bcode" };

	public BcodeMain() {
		init();
		registerMainStore(store);
		registerStore(store);
	}

	public void init() {
		this.add(initWeigetx());
	}

	public void complete() {
		super.complete();
		print.addListener(Events.Select, getListener());
		getToolbarll().add(print);
	}

	Bcode_HR bcode_hr;
	Bcode_EmployeeChange employ;
	Bcode_salary bcode_salary;
	Bcode_Employment bcode_employment;
	Bcode_SalaryRecord bcode_salaryrecord;
	Bcode_Contract bcode_Contract;
	Bcode_Family bcode_family;
	Bcode_Cer bcode_cer;

	private Widget initWeigetx() {
		tabpanel = new TabPanel();
		setTabpanel(tabpanel);
		tabpanel.setPlain(true); 
		createStore();

		HorizontalPanel h = new HorizontalPanel();
		
		ContentPanel cpanel = new ContentPanel();
		cpanel.add(getTreePanel());
		cpanel.setHeaderVisible(false);
		
		cb = new CheckBox();
		cb.addListener(Events.Change, getListener());
		toolbar.add(new Label("离职"));
		toolbar.add(cb);
		toolbar.add(new SeparatorToolItem());  
	    toolbar.add(new FillToolItem());  
		toolbar.add(new Label("查找"));
		toolbar.add(cread_field());
		
		cpanel.setTopComponent(toolbar);
		
		h.add(cpanel);
		h.setSpacing(2);
		TabItem ti = new TabItem();
		bcode_hr = new Bcode_HR(store);
		// 注册store
		registerStore(bcode_hr.getStore());
		ti.setStyleName("tabitem_bg_color");
		ti.add(bcode_hr);
		ti.setText("雇员");
		ti.setScrollMode(Scroll.AUTO);
	//	toolbar.setAlignment(HorizontalAlignment.RIGHT);

		TabItem ti2 = new TabItem();
		ti2.setStyleName("tabitem_bg_color");
		ti2.setText("雇用工作");
		bcode_employment = new Bcode_Employment(this);
		registerStore(bcode_employment.getStore());

		ti2.add(bcode_employment);
		ti2.setScrollMode(Scroll.AUTO);
			
		TabItem ti_add1 = new TabItem();
		ti_add1.setText("薪金");
		ti_add1.setStyleName("tabitem_bg_color");
		bcode_salary = new Bcode_salary(this);
		registerStore(bcode_salary.getBcode_EmploymentStore());
		registerStore(bcode_salary.get_bcode_Allowance_Store());
		ti_add1.add(bcode_salary);
		ti_add1.setScrollMode(Scroll.AUTO);
		
		TabItem ti_add = new TabItem();
		ti_add.setText("发薪记录");
		ti_add.setStyleName("tabitem_bg_color");
		bcode_salaryrecord = new Bcode_SalaryRecord(store);
		ti_add.add(bcode_salaryrecord);
		ti_add.setScrollMode(Scroll.AUTO);
		
		TabItem ti3 = new TabItem();
		ti3.setText("工作经历");
		ti3.setStyleName("tabitem_bg_color");
		employ = new Bcode_EmployeeChange(this);
		ti3.add(employ);
		registerStore(employ.getStore2());
		registerStore(employ.getJobhis_a_store());
		ti3.setScrollMode(Scroll.AUTO);
		
		TabItem ti_add3 = new TabItem();
		ti_add3.setText("合同");
		ti_add3.setStyleName("tabitem_bg_color");
		bcode_Contract = new Bcode_Contract(store);
		ti_add3.add(bcode_Contract);
		ti_add3.setScrollMode(Scroll.AUTO);

		TabItem ti_add2 = new TabItem();
		ti_add2.setText("培训");
		ti_add2.setStyleName("tabitem_bg_color");
		Bcode_Train Bcode_Train = new Bcode_Train(store);
		ti_add2.add(Bcode_Train);
		ti_add2.setScrollMode(Scroll.AUTO);

		TabItem ti4 = new TabItem();
		ti4.setText("资历");
		ti4.setStyleName("tabitem_bg_color");
		bcode_cer = new Bcode_Cer(store);
		registerStore(bcode_cer.get_bcode_cer_Store());
		registerStore(bcode_cer.get_bcode_quality_Store());
		registerStore(bcode_cer.get_bcode_skill_Store());
		registerStore(bcode_cer.getbcode_educationStore());
		ti4.add(bcode_cer);
		ti4.setScrollMode(Scroll.AUTO);

		TabItem ti5 = new TabItem();
		ti5.setText("家庭信息");
		ti5.setStyleName("tabitem_bg_color");
		bcode_family = new Bcode_Family(store);
		registerStore(bcode_family.get_bcode_famify_Store());
		registerStore(bcode_family.get_bcode_Spouse_Store());
		ti5.add(bcode_family);
		ti5.setScrollMode(Scroll.AUTO);

		TabItem ti6 = new TabItem();
		ti6.setText("其他资料");
		ti6.setStyleName("tabitem_bg_color");
		Bcode_Extdata bcode_extdata = new Bcode_Extdata(store);
		registerStore(bcode_family.getStore());
		ti6.setScrollMode(Scroll.AUTO);
		ti6.add(bcode_extdata);
		
		TabItem softManager = new TabItem();
		softManager.setText("ＩＴ管理");
		Bcode_SoftManager bcode_SM = new Bcode_SoftManager(store);
		registerStore(bcode_SM.getStore());
		softManager.add(bcode_SM);

		tabpanel.add(ti);
		tabpanel.add(ti2);
		tabpanel.add(ti_add1);
		tabpanel.add(ti_add);
		tabpanel.add(ti3);
		tabpanel.add(ti_add3);
		tabpanel.add(ti_add2);
		tabpanel.add(ti4);
		tabpanel.add(ti5);
		tabpanel.add(softManager);
		tabpanel.add(ti6);
		tabpanel.setSelection(ti2);

		h.add(tabpanel);

		Tools.setResize(treepanel, 0.21, 0.95); // 左边grid的宽度
		Tools.setResize(tabpanel, 0.79, 0.99);
		Tools.setResize(cpanel, 0.21, 0.99);

//		bcode_hr.addReFreshStore(employ.getJobhis_a_store());
		return h;
	}
	
	public BaseStore<BusiObject> getStore() {
		if (store == null) {
			return createStore();
		} else {
			return store;
		}
	}

	/**
	 * 用于建立一个store，用于作为所有的tab中的父store
	 * 
	 * @return 一个主store，用于关联所有的tab中的数据
	 */
	private BaseStore<BusiObject> createStore() {
		ModelType type = new ModelType();
		type.setRoot("records");
		type.setRecordName("record");
		type.setTotalName("totle");
		type.addField("icode");
		type.getField("icode").setType(Long.class);
		type.addField("bcode");
		type.getField("bcode").setType(String.class);
		type.addField("bname");
		type.getField("bname").setType(String.class);
		type.addField("sex");
		type.getField("sex").setType(String.class);
		type.addField("bird");
		type.getField("bird").setType(Date.class);
		type.addField("age");
		type.getField("age").setType(Integer.class);
		type.addField("hr_firstname_zh");
		type.getField("hr_firstname_zh").setType(String.class);
		type.addField("hr_lastname_zh");
		type.getField("hr_lastname_zh").setType(String.class);
		type.addField("hr_firstname_en");
		type.getField("hr_firstname_en").setType(String.class);
		type.addField("hr_lastname_en");
		type.getField("hr_lastname_en").setType(String.class);
		type.addField("hr_nametitle");
		type.getField("hr_nametitle").setType(String.class);
		type.addField("hr_name_Alias");
		type.getField("hr_name_Alias").setType(String.class);
		type.addField("hr_idcard_limitdate");
		type.getField("hr_idcard_limitdate").setType(Date.class);
		type.addField("hr_idcard_addr");
		type.getField("hr_idcard_addr").setType(String.class);
		type.addField("hr_idcard_abroad_addr");
		type.getField("hr_idcard_abroad_addr").setType(String.class);
		type.addField("hr_idcard_abroad_no");
		type.getField("hr_idcard_abroad_no").setType(String.class);
		type.addField("hr_nationality");
		type.getField("hr_nationality").setType(String.class);
		type.addField("hr_Marital_status");
		type.getField("hr_Marital_status").setType(String.class);
		type.addField("hr_marital_date");
		type.getField("hr_marital_date").setType(Date.class);
		type.addField("hr_Census_Province");
		type.getField("hr_Census_Province").setType(String.class);
		type.addField("hr_Census_city");
		type.getField("hr_Census_city").setType(String.class);
		type.addField("hr_City_Township");
		type.getField("hr_City_Township").setType(String.class);
		type.addField("hr_Census_area");
		type.getField("hr_Census_area").setType(String.class);
		type.addField("hr_social_no");
		type.getField("hr_social_no").setType(String.class);
		type.addField("hr_status");
		type.getField("hr_status").setType(String.class);
		type.addField("hr_entry_date");
		type.getField("hr_entry_date").setType(Date.class);
		type.addField("up_department");
		type.getField("up_department").setType(String.class);
		type.addField("hr_bankno");
		type.addField("hr_bankcard");
		type.addField("shortcode");
		type.addField("groupicode");
		type.addField("Identifier");
		type.getField("Identifier").setType(String.class);
		type.addField("systemid");
		type.getField("systemid").setType(String.class);

		type.addField("salaryploy");
		
		type.addField("basesalary");
		type.getField("basesalary").setType(Double.class);
		type.addField("tax_tactics");

		StoreInfo info = new StoreInfo();
		info.setTablename("bcode");
		info.setKeyname(new String[] { "icode" });
		info.setSql("select bcode.* from bcode,bcode_job where bcode.bcode=bcode_job.bcode and bcode_job.employstatus='在职' ");
		store = new BaseStore<BusiObject>(type, info) {
			public boolean trySave() {
				boolean success = false;
				
				if (bcode_hr.C_num3.getRawValue().equals("") || bcode_hr.C_num4.getRawValue().equals("") || bcode_employment.C_dep.getRawValue().equals("")
						|| bcode_employment.C_num27.getRawValue().equals("") || bcode_employment.in_work.getRawValue().equals("")
						|| bcode_employment.C_num20.getRawValue().equals("") || bcode_hr.C_numcx.getRawValue().equals("")) {
					MessageBox.confirm("提示	","数据必填项没有完全填写，选择“是”继续填写，选择“否”不保存当前人。",
							new Listener<MessageBoxEvent>() {
								public void handleEvent(MessageBoxEvent be) {
									if (be.getButtonClicked().getText().equals(
											GXT.MESSAGES.messageBox_yes())) {
									} else {
										if(bcode_employment.C_num8.getRawValue().equals("在职") || bcode_employment.C_num8.getRawValue().equals("离职")) {
											MessageBox.alert("提示", "不能对在职/离职的人员进行删除操作，所以操作失败，谢谢", null);
											return;
										}
										String bcode = treepanel.getSelectionModel()
												.getSelectedItem().get("bcode");
										deleteBcode(bcode);
									}
								}
							});
				} else {
					success = super.trySave();
				}
				
				return success;
			}
		};
		return store;
	}

	BaseTreeStore<TreeModel> treeStore = null;

	private TreePanel<TreeModel> getTreePanel() {
		if (treepanel == null) {
			initTreePanel();
		}
		return treepanel;
	}

	private BaseTreeStore<TreeModel> getTreeStore() {
		if (treeStore == null) {
			initTreeStore();
		}
		return treeStore;
	}

	/**
	 * 新建一个用于treepanel的store
	 * 
	 * @return 一个treeStore
	 */
	private BaseTreeStore<TreeModel> initTreeStore() {
		StoreInfo info = new StoreInfo();
		String sql2 = "select isnull(systemid,'00')+a.bcode as systembcode,a.bname,employno,a.bcode as bcode,a.sex from bcode as a,bcode_job as b where a.bcode=b.bcode " +
					"and isnull(b.employstatus,'') <> '离职' union all select id as systembcode,name + ' ' + cast((select count(1) from  bcode a inner join bcode_job b on a.bcode = b.bcode where isnull(b.employstatus,'') <> '离职' and jobtype != '临时工' and systemid like id + '%') as varchar(10)) as bname,null as employno,null as bcode,null as sex from systeminfo order by systembcode";
		info.setSql(sql2);
		info.setTreesortcolumn("systembcode");
		info.setKeyname(new String[] { "icode" });
		info.setTablename("bcode");
		treeStore = new BaseTreeStore<TreeModel>(info, false);
		treeStore.load();
		return treeStore;
	}

	/**
	 * 用于初始化treepanel，并根据用户选中的子树来同步更新tabpanel中的数据。
	 */
	private void initTreePanel() {
		treepanel = new TreePanel<TreeModel>(getTreeStore());
		treepanel.setDisplayProperty("bname");
		treepanel.setCheckable(false);
		treepanel.setAutoLoad(true);
		treepanel.getStyle().setNodeOpenIcon(
				AbstractImagePrototype.create(BaseResource.INSTANCE
						.tree()));
		treepanel.getStyle().setNodeCloseIcon(
				AbstractImagePrototype.create(BaseResource.INSTANCE
						.tree()));
		treepanel.getStyle().setLeafIcon(
				AbstractImagePrototype.create(BaseResource.INSTANCE.bcode()));
		treepanel.setLabelProvider(defaultStringProvider);
		treepanel.setCheckNodes(CheckNodes.LEAF);
		treepanel.setCheckStyle(CheckCascade.NONE);
		treepanel.setCaching(true);
		treepanel.addListener(Events.OnClick,
				new Listener<TreePanelEvent<TreeModel>>() {
					public void handleEvent(TreePanelEvent<TreeModel> be) {
						if (be.getItem() != null) {
							if (be.getItem().getChildCount() == 0) {
								treeDepId = be.getItem().get("bcode").toString();
								if (be.getItem().get("bname") == null) {
									treeDepName = "";
								} else {
									treeDepName = be.getItem().get("bname").toString();
								}
								employ.getEmployeeChange(treeDepId, treeDepName);
								store.getStoreinfo().setSql("select * from bcode where bcode='" + treeDepId + "'");
								store.reload();
								TabItem item = tabpanel.getItem(0);
								String employno = be.getItem().get("employno").toString();
								item.setText(item.getText().split("-")[0] + "-" + employno + treeDepName);
							} 
						}
					}
				});
		Menu contextMenu = new Menu();

		MenuItem insert = new MenuItem();
		insert.setText("添加雇员");
		insert.setIcon(AbstractImagePrototype.create(BaseResource.INSTANCE.addrecord()));
		insert.addSelectionListener(new SelectionListener<MenuEvent>() {
			public void componentSelected(MenuEvent ce) {
				TreeModel model = treepanel.getSelectionModel().getSelectedItem();
				if (model.isLeaf()) {
					MessageBox.alert("提示", "只能对部门进行添加人员，请重新操作，谢谢", null);
					return;
				}
				addBcode(model);
			}
		});
		contextMenu.add(insert);
		
		MenuItem delete = new MenuItem();
		delete.setText("删除雇员");
		delete.setIcon(AbstractImagePrototype.create(BaseResource.INSTANCE.deleterecord()));
		delete.addSelectionListener(new SelectionListener<MenuEvent>() {
			public void componentSelected(MenuEvent ce) {
				final TreeModel model = treepanel.getSelectionModel().getSelectedItem();
				if (!model.isLeaf())
					return;
				
				String sql = "select employstatus from bcode_job where bcode='"
					+ model.get("bcode")
					+ "'";
			GWT.log(sql);
			final com.base.myproject.client.tools.GreetingServiceAsync greetingService2 = GWT
					.create(com.base.myproject.client.tools.GreetingService.class);
			greetingService2.getDataSet(sql,new AsyncCallback<DataSet>() {

				@Override
				public void onFailure(Throwable caught) {
					
				}

				@Override
				public void onSuccess(DataSet result) {
					if (result.getValue(0, "employstatus") != null && result.getValue(0, "employstatus").equals("在职")) {
						MessageBox.alert("提示", "不能对在职的员工进行删除操作，此次操作不成功！", null);
						return;
					}
					
					MessageBox.prompt("安全提示", "你确定要删除[" + model.get("bname") + "]在本系统的所有资料吗？确定请再次输入你的密码，否则退出，谢谢",
							new Listener<MessageBoxEvent>() {
								public void handleEvent(MessageBoxEvent be) {
									Button btn = be.getButtonClicked();
									if (btn.getText().equals("确定")) {
										String bcode = Env.getEnvbcode();
										String sql = "select password from bcode where bcode='"
												+ bcode
												+ "' and password='"
												+ be.getValue() + "'";
										GWT.log(sql);
										final com.base.myproject.client.tools.GreetingServiceAsync greetingService2 = GWT
												.create(com.base.myproject.client.tools.GreetingService.class);
										greetingService2.getDataSet(sql,new AsyncCallback<DataSet>() {

											@Override
											public void onFailure(Throwable caught) {
												
											}

											@Override
											public void onSuccess(DataSet result) {
												if (result.getRowCount() > 0) {
													String bcode = model.get("bcode");
													deleteBcode(bcode);
												} else {
													Info.display("提示消息","您输入的密码有误,所以这次删除失败!");
												}
											}
										});
									}
								}
							});
				}
			});
			}
		});

		contextMenu.add(delete);

		MenuItem refresh = new MenuItem();
		refresh.setText("刷新雇员");
		refresh.setIcon(AbstractImagePrototype.create(BaseResource.INSTANCE.fresh()));
		refresh.addSelectionListener(new SelectionListener<MenuEvent>() {
			public void componentSelected(MenuEvent ce) {
				treeStore.load();
			}
		});
		contextMenu.add(refresh);
		treepanel.setContextMenu(contextMenu);
	}

	/**
	 * 用于从用于新增一个雇员和向treepanel新增加一条记录，添加的bcode为随机生成的，并向bcode_job表新增加
	 * 一条记录，用于防止用于在点击from的时候老提示要不要新增加记录。最后在添加成功的同时刷新tabpanel。
	 * 
	 * @param model
	 *            将要添加雇员的部门
	 * @param value
	 *            添加的雇员的名字
	 * @return 添加是否成功，现在这个返回值还没用处
	 */
	private boolean addBcode(TreeModel model) {
		TreeModel child = new BaseTreeModel();
		final String maxBcode = "A" + String.valueOf(Random.nextInt());  //加个“A”，主要用于区别临时员工还是在职员工，也有个作用是为了别让自动生成的编号大于正式编号
		child.set("bcode", maxBcode);
		treeStore.add(model, child, false);
		treepanel.setExpanded(model, true);
		employ.getEmployeeChange(maxBcode, "");

		// 二条sql语句，主要为了解决tabpanel里面的form如果数据为null会提示添加新记录的问题，也可以用sql这条语句
		// 但是from为null就会提示添加新记录
		String sql = "insert into bcode(hr_entry_date, bcode,hr_City_Township) values(convert(varchar(12),getdate(),111),'"
				+ maxBcode + "','城镇')";
		String sql1 = "insert into bcode_job(bcode) values('" + maxBcode + "')";

		CommandSyncsql csql = new CommandSyncsql();// 执行sql命令
		csql.getV().add(sql);
		csql.getV().add(sql1);
		if (csql.getV().size() <= 0) {
			return false;
		} else {
			CommandSyncContainer list = new CommandSyncContainer();// 可以添加的命令
			list.add(csql);

			GreetingServiceAsync greetingService = GWT
					.create(GreetingService.class);
			greetingService.SendCommandSync(list,
					new AsyncCallback<CommandSyncReturnObject>() {
						@Override
						public void onFailure(Throwable caught) {
							MessageBox.alert("错误", "网络连接失败", null);
						}

						@Override
						public void onSuccess(CommandSyncReturnObject result) {
							if (result.isB()) {
								String oldSql = store.getStoreinfo().getSql();
								store.getStoreinfo().setSql(
										"select * from bcode where bcode='" + maxBcode
												+ "'");
								store.reload();
								store.getStoreinfo().setSql(oldSql);
								Info.display("提示信息", "添加雇员成功");
							} else {
								MessageBox.alert("错误", "错误，错误代码"
										+ result.getMessage(), null);

							}
						}
					});
		}
		return true;
	}

	/**
	 * 通过对bname和bcode进行查询，先排除bname和bcode为空的记录，不然会报错，然后再对bname和bcode进行
	 * bname和bcode以查询条件为开始的匹配查询
	 * 
	 * @return 所有符合条件的子树
	 */
	public StoreFilterField<TreeModel> cread_field() {
		StoreFilterField<TreeModel> field = new StoreFilterField<TreeModel>() {
			@Override
			protected boolean doSelect(Store<TreeModel> store,
					TreeModel parent, TreeModel record, String property,
					String filter) {
				String name = record.get("bname");
				String identifier = record.get("employno");
				if (name != null) {
					name = name.toLowerCase();
					if (name.indexOf(filter.toLowerCase()) != -1) {
						return true;
					}
				}
				if (identifier != null) {
					identifier = identifier.toLowerCase();
					if(identifier.indexOf(filter.toLowerCase()) != -1) {
						return true;
					}
				}
				return false;
			}

			@Override
			protected void onFilter() {
				super.onFilter();
			}

		};
		field.setWidth(100);
		field.setToolTip("对人员进行查找");
		field.bind(treeStore);
		return field;
	}

/**
	 * 重写save方法，当点击保存的时候，把相关的子树的bcode改为from中的值，为了能继续查询，但是树中的显示
	 * 没法更新，可以用treestore的load方法更新，但是太慢，所有选择不更新，但能查的下面的方法。
	 * 
	 * @see com.base.myproject.client.ui.BaseJDBCPanel#save()
	 *//*
	protected void save() {
		super.save();
		if(bcode_hr.C_num2.getValue() != null) {
			
		}
	}*/

	/************************************/
	int sync = 0;
	Listener<BaseEvent> defaultlistener;

	Listener<BaseEvent> getListener() {
		if (defaultlistener != null) {
			return defaultlistener;
		}

		defaultlistener = new Listener<BaseEvent>() {
			public void handleEvent(BaseEvent be) {
				sync++;
				try {
					if (sync == 1) {
						if (be.getType() == Events.Select) {
							if(be.getSource().equals(print)){
								print();
							}
						}else if (be.getType() == Events.Change){
							if(be.getSource().equals(cb)){
								if(cb.getValue()){
									StoreInfo info = treeStore.getStoreinfo();
									String sql = "select isnull(systemid,'00')+a.bcode as systembcode,a.bname,employno,a.bcode as bcode,a.sex from bcode as a,bcode_job as b where a.bcode=b.bcode " +
									"and isnull(b.employstatus,'') = '离职' union all select id as systembcode,name + ' ' + cast((select count(1) from  bcode a inner join bcode_job b on a.bcode = b.bcode where isnull(b.employstatus,'') = '离职' and jobtype != '临时工' and systemid like id + '%') as varchar(10)) as bname,null as employno,null as bcode,null as sex from systeminfo order by systembcode";
									info.setSql(sql);
									treeStore.load();
								}else{
									StoreInfo info = treeStore.getStoreinfo();
									String sql = "select isnull(systemid,'00')+a.bcode as systembcode,a.bname,employno,a.bcode as bcode,a.sex from bcode as a,bcode_job as b where a.bcode=b.bcode " +
									"and isnull(b.employstatus,'') <> '离职' union all select id as systembcode,name + ' ' + cast((select count(1) from  bcode a inner join bcode_job b on a.bcode = b.bcode where isnull(b.employstatus,'') <> '离职' and jobtype != '临时工' and systemid like id + '%') as varchar(10)) as bname,null as employno,null as bcode,null as sex from systeminfo order by systembcode";
									info.setSql(sql);
									treeStore.load();
								}
							}
						}
					}
				} finally {
					sync--;
				}
			}
		};

		return defaultlistener;
	}

	/**
	 * 得到最大的员工编号，再加1作为新的员工编号
	 *//*
	public void selectMaxBcode(final TreeModel model) {
		// 这条sql用于去得到bcode表中所有纯数字的员工编号
		String sql = "select max(case when isnumeric(bcode)=1 then bcode else '0001' end) as max from bcode";
		final GreetingServiceAsync greetingService = GWT
				.create(com.base.myproject.client.tools.GreetingService.class);
		greetingService.getDataSet(sql, new AsyncCallback<DataSet>() {
			@Override
			public void onFailure(Throwable caught) {
			}

			@Override
			public void onSuccess(DataSet result) {
				String max_ = result.getValue(0, "max");
				int num_ = Integer.parseInt(max_);
				num_ += 1;
				max_ = String.valueOf(num_);
				if (max_.length() == 1) {
					max_ = "000" + max_;
				} else if (max_.length() == 2) {
					max_ = "00" + max_;
				} else if (max_.length() == 3) {
					max_ = "0" + max_;
				} else {
					max_ = max_;
				}
				
				addBcode(model, max_);
			}
		});
	}*/
	
	/**
	 * @param lists
	 *            要执行的sql语句
	 * @return 执行成功与否 执行所有的delete语句，完全申请表的提交操作
	 */
	public boolean deleteBcode(String bcode) {
		List<String> lists = new ArrayList<String>();
		String where = " where bcode = '" + bcode + "'";
		String sql1 = "delete from bcode" + where;//雇员资料
		String sql2 = "delete from bcodeaddr" + where;//联络方式
		String sql3 = "delete from hr_idCard" + where;//身份证件
		String sql4 = "delete from bcode_job" + where;//雇员工作信息
		String sql5 = "delete from bcode_Allowance" + where;//雇员薪金记录
		String sql7 = "delete from hr_EmployeeChange" + where;//工作记录
		String sql8 = "delete from bcode_job_his_a" + where;//早前工作记录
		String sql6 = "delete from hr_bcode_contract" + where;//雇员合同
		String sql10 = "delete from bcode_education" + where;//雇员教育程度
		String sql11 = "delete from bcode_quality" + where;//雇员资格表
		String sql12 = "delete from bcode_skill" + where;//雇员技能表
		String sql13 = "delete from bcode_cer" + where;//雇员证件表
		String sql14 = "delete from bcode_Spouse" + where;//配偶
		String sql15 = "delete from bcode_famify" + where;//家庭成员
		String sql16 = "delete from bcode_contact" + where;//紧急联络方式
		String sql17 = "delete from bcode_extdata" + where;//其他资料
		String sql18 = "delete from bcode_images" + where;//雇员图片
		String sql19 = "delete from hr_social_basedetail" + where;//社保基数
		String sql20 = "delete from USERINFO" + where; // USERINFO表的
				
		lists.add(sql1);
		lists.add(sql2);
		lists.add(sql3);
		lists.add(sql4);
		lists.add(sql5);
		lists.add(sql6);
		lists.add(sql7);
		lists.add(sql8);
		lists.add(sql10);
		lists.add(sql11);
		lists.add(sql12);
		lists.add(sql13);
		lists.add(sql14);
		lists.add(sql15);
		lists.add(sql16);
		lists.add(sql17);
		lists.add(sql18);
		lists.add(sql19);
		lists.add(sql20);
		
		CommandSyncsql csql = new CommandSyncsql();// 执行sql命令
		for (String sql : lists) {
			csql.getV().add(sql);
		}
		if (csql.getV().size() <= 0) {
			return false;
		} else {
			CommandSyncContainer list = new CommandSyncContainer();// 可以添加的命令
			list.add(csql);

			GreetingServiceAsync greetingService = GWT
					.create(GreetingService.class);
			greetingService.SendCommandSync(list,
					new AsyncCallback<CommandSyncReturnObject>() {
						@Override
						public void onFailure(Throwable caught) {
							MessageBox.alert("错误", "网络连接失败", null);
						}

						@Override
						public void onSuccess(CommandSyncReturnObject result) {
							if (result.isB()) {
								Info.display("提示信息", "去除雇员成功");
								treeStore.load();
								store.reload();
							} else {
								MessageBox.alert("错误", "错误，错误代码"
										+ result.getMessage(), null);

							}
						}
					});
		}
		return true;
	}

	/**
	 * @author cxghiu410@gmail.com 对树的显示进行转换，让树既显示bname又显示bcode
	 * @param null
	 */
	class BaseModelStringProvider<M extends ModelData> implements
			ModelStringProvider<M> {
		public String getStringValue(M model, String property) {
			Object o = model.get(property);
			if (o == null) {
				return "";
			} else {
				return model.get("bname")
						+ "\t"
						+ (model.get("employno") == null ? "" : model.get("employno"));
			}
		}

	}
	
	public void print(){
		if(store.getValue("bcode") == null)
		{
			MessageBox.alert("错误", "未选择要打印的雇员。", null);
			return;
		}
		String bcode =store.getValue("bcode").toString();
		PrintBcodeDialog printdialog = new PrintBcodeDialog("hr_Jsp/bcodePrintImages.jsp?bcode="+bcode) ;
		printdialog.show();	
	}

	public TabPanel getTabpanel() {
		return tabpanel;
	}

	public void setTabpanel(TabPanel tabpanel) {
		this.tabpanel = tabpanel;
	}
	
	public String getTreeDepId() {
		return treeDepId;
	}

	public String getTreeDepName() {
		return treeDepName;
	}

}
